package com.cls.business.strategy.impl.export;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.cls.business.strategy.ExpertExcel8ReviewRowStrategy;
import com.cls.business.vo.review.result.DeclTopicVO;
import com.cls.business.vo.review.result.ExpertGroupVO;
import com.cls.business.vo.review.result.ExpertScoreVO;
import com.cls.business.vo.review.result.ReviewResultExportVO;
import com.cls.system.mapper.SysDictMapper;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 这个是导出结项验收课题成果鉴定结果统计表的自定义
 */
@RequiredArgsConstructor
@Service("itemExportExcelImpl")
public class ItemExportExcelImpl implements ExpertExcel8ReviewRowStrategy {

    private final SysDictMapper dictMapper;

    /**
     * 写入所有的课题
     *
     * @param writer
     * @param declTopics
     * @param reviewResultIndex
     * @param expertSize
     */
    @Override
    public void writeDeclTopic(ExcelWriter writer, LinkedList<DeclTopicVO> declTopics, int reviewResultIndex, int expertSize) {
        //循环每一行数据
        for (int declTopicIndex = 0; declTopicIndex < declTopics.size(); declTopicIndex++) {
            DeclTopicVO declTopicVO = declTopics.get(declTopicIndex);
            ArrayList<Serializable> declTopicRow = CollUtil.newArrayList((declTopicIndex + 1), declTopicVO.getItemCode(), declTopicVO.getDeclTopicName(), declTopicVO.getUserName(), declTopicVO.getZwzc());
            StringBuffer opinion = new StringBuffer();
            for (int i = 0; i < expertSize; i++) {
                LinkedList<ExpertScoreVO> scores = declTopicVO.getScores();
                if (scores.size() > i) {
                    ExpertScoreVO expertScoreVO = scores.get(i);
                    declTopicRow.add(expertScoreVO.getScore());
                    opinion.append(expertScoreVO.getExpertName() + ": " + expertScoreVO.getOpinion() + "\n");
                } else {
                    declTopicRow.add("");
                }
            }
            declTopicRow.add(declTopicVO.getPlanScore());
            declTopicRow.add(declTopicVO.getLevelType());
            declTopicRow.add(opinion.toString());
            writer.writeRow(declTopicRow);
            writer.setRowHeight(writer.getCurrentRow() - 1, 30);
        }
    }

    /**
     * 专家分组标题写入
     *
     * @param reviewResultExportVO 当前的课题
     * @param expertGroupVO        专家分组
     * @param expertGropsIndex     当前的index
     * @param headMergeSize        要合并多少单元格
     */
    @Override
    public void writeExpertGroupTitle(ExcelWriter excelWriter, ReviewResultExportVO reviewResultExportVO, ExpertGroupVO expertGroupVO, int expertGropsIndex, int headMergeSize,CellStyle expertGroupTitleCellStyle) {
        int currentRow = excelWriter.getCurrentRow();
        Row orCreateRow = excelWriter.getOrCreateRow(currentRow);
        //注意单元格必须创建 否则自定义样式不生效
        Cell cell = orCreateRow.createCell(0);
        excelWriter.merge(headMergeSize);
        cell.setCellStyle(expertGroupTitleCellStyle);
        //根据typecodes查询所有的name
        String typeNames = dictMapper.findNameByGroupConcat(reviewResultExportVO.getSubjectTypeCodes());
        cell.setCellValue(typeNames + "第" + (expertGropsIndex + 1) + "组（" + expertGroupVO.getDeclTopics().size() + "项）");
        //指向下一行
        excelWriter.setCurrentRow(currentRow + 1);
    }

    /**
     * 写文章的总标题
     *
     * @param excelWriter
     * @param headMergeSize
     */
    @Override
    public void writeExcleTile(ExcelWriter excelWriter, int headMergeSize, String title) {
        Row orCreateRow = excelWriter.getOrCreateRow(0);
        //注意单元格必须创建 否则自定义样式不生效
        Cell cell = orCreateRow.createCell(0);
        //必须设置当前行，否则下面的merge就在最后一行啦。
        excelWriter.setCurrentRow(0);
        excelWriter.merge(headMergeSize);
        //这里需要新创建样式
        CellStyle cellStyle = excelWriter.createCellStyle();
        cell.setCellValue(title);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        Font headFont = excelWriter.createFont();
        headFont.setBold(true);
        headFont.setFontName("宋体");
        //设置字体大小
        headFont.setFontHeightInPoints((short) 20);
        cellStyle.setFont(headFont);
        cell.setCellStyle(cellStyle);
        //设置行高
        excelWriter.setRowHeight(0, 50);

    }

    /**
     * 课题标题不存在  可以不写
     *
     * @param excelWriter
     * @param headMergeSize
     * @param reviewResultExportVO
     */
    @Override
    public void writeTopicHead(ExcelWriter excelWriter, int headMergeSize, ReviewResultExportVO reviewResultExportVO) {

    }
}
